package com.cy.ppj.service.message.impl;

import com.cy.ppj.comm.annotation.Message;
import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.dao.mapper.message.MessageTemplateMapper;
import com.cy.ppj.dao.mapper.order.OrderGoodsExtraInfoMapper;
import com.cy.ppj.model.enums.MessageType;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.message.MessageRecordService;
import com.cy.ppj.service.message.MessageService;
import com.cy.ppj.service.message.MessageTemplateService;
import com.cy.ppj.service.wechat.BankmaWechatService;
import com.cy.ppj.service.wechat.WechatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: Ocean
 * @create: 2018-08-09 17:06
 **/
@Service("messageService")
@Primary
public class MessageServiceImpl extends BaseService implements MessageService {

    @Autowired
    protected MessageTemplateMapper messageTemplateMapper;
    @Autowired
    protected WechatService wechatService;
    @Autowired
    protected BankmaWechatService bankmaWechatService;
    @Autowired
    protected MessageTemplateService messageTemplateService;
    @Autowired
    protected MessageRecordService messageRecordService;
    @Autowired
    protected OrderGoodsExtraInfoMapper orderGoodsExtraInfoMapper;

    private static Map<MessageType, MessageServiceImpl> messageServiceMap = new HashMap<>();

    @PostConstruct
    public void initMap() {
        Message message = this.getClass().getAnnotation(Message.class);
        if (message == null) {
            return;
        }
        messageServiceMap.put(message.type(), this);
    }

    @Override
    @Async(Constants.ASYNC)
    public void sendMessage(String message, MessageType type) {
        //执行发送消息的方法
        if(messageServiceMap.get(type) != null) {
            messageServiceMap.get(type).execute(message);
        }
    }

    /**
     * 真正执行发送消息的方法
     *
     * @param message
     */
    public void execute(String message) {

    }
}
